package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.micrometer.common.lang.NonNullApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.annotation.Target;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */

    @Operation(summary = "员工登录", description = "返回员工信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取用户信息"),
            @ApiResponse(responseCode = "404", description = "用户未找到")
    })
    @Tag(name = "员工管理", description = "员工相关操作")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功退出")
    })
    @Operation(summary = "员工退出", description = "返回员工退出信息")
    @Tag(name = "员工管理", description = "员工相关操作")
    public Result<String> logout() {

        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "员工删除",description = "返回员工删除信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功删除员工")
    })
    public Result<String> delete(@PathVariable Long id) {
        log.info("员工删除:{}", id);
        employeeService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功添加员工"),
            @ApiResponse(responseCode = "401", description = "当前员工已存在")
    })
    @Operation(summary = "新增员工",description = "返回新增员工信息")
    @Tag(name = "员工管理", description = "员工相关操作")
    public Result<EmployeeDTO> save(@RequestBody EmployeeDTO employeeDTO){
//        System.out.println("当前线程id:"+Thread.currentThread().getId());

        log.info("新增员工:{}",employeeDTO);
//      employeeService.save(employeeDTO);
//      return Result.success();
        employeeDTO=employeeService.save(employeeDTO);
        return Result.success(employeeDTO);
    }

    /**
     * 分页查询
     * @param employeePageQueryDTO 员工查询条件
     * @return PageResult
     */
    @GetMapping("/page")
    @Operation(summary = "员工分页查询",description = "返回员工分页查询信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功获取员工分页信息")
    )
    @Tag(name = "员工管理", description = "员工相关操作")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询");
        PageResult pageResult=employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 启用禁用员工账号
     * @param status 状态
     * @param id 员工id
     * @return Result
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "启用禁用员工账号",description = "返回启用禁用员工账号信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功启用禁用员工账号信息")
    )
    @Tag(name = "员工管理", description = "员工相关操作")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("启用禁用员工账号:{},{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id 员工id
     * @return Result
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询员工信息", description ="返回查询到的员工信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200", description = "成功获取员工信息")
    )
    @Tag(name = "员工管理", description = "员工相关操作")
    public Result<Employee> getById(@PathVariable Integer id){
        log.info("根据id查询员工信息:{}",id);
        Employee employee=employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    @Operation(summary = "编辑员工信息",description = "返回员工信息修改信息")
    @ApiResponses(
            @ApiResponse(responseCode = "200",description = "成功修改员工信息")
    )
    @Tag(name = "员工管理", description = "员工相关操作")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("员工信息修改:{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
